import { _decorator, Component, Node, Label, UITransform, Color, director, Canvas, Graphics, Vec3, tween, Vec2 } from 'cc';

export class Toast {
    private static _inst: Toast;
    public static get inst(): Toast {
        if (this._inst == null) {
            this._inst = new Toast();
        }
        return this._inst;
    }

    //一个简单的tost组件，用法：
    // let Toast = reqire('Toast.js')
    // Toast(text,{gravity,duration,bg_color})
    //text:要显示的字符串
    //gravity(可选):位置，String类型，可选值('CENTER','TOP','BOTTOM'),默认为'CENTER'
    //duration(可选):时间，Number类型，单位为秒，默认1s
    //bg_color(可选):颜色，cc.color类型，默认cc.color(102, 102, 102, 200)
    showToast(
        text = "",
        {
            gravity = "CENTER",
            duration = 1,
            bg_color = new Color(102, 102, 102, 200)
        } = {}
    ) {
        // Canvas
        const canvas = director.getScene()?.getComponentInChildren(Canvas);
        if (!canvas) return;
        const width = canvas.node.getComponent(UITransform)?.width ?? 0;
        const height = canvas.node.getComponent(UITransform)?.height ?? 0;

        // Text Node and Label
        const textNode = new Node();
        const textLabel = textNode.addComponent(Label);
        textLabel.horizontalAlign = Label.HorizontalAlign.CENTER;
        textLabel.verticalAlign = Label.VerticalAlign.CENTER;
        textLabel.fontSize = 42;
        textLabel.string = text;

        const transform = textNode.addComponent(UITransform);

        // Resize for long text
        if (text.length * textLabel.fontSize > (width * 3) / 5) {
            transform.width = (width * 3) / 5;
            textLabel.overflow = Label.Overflow.RESIZE_HEIGHT;
        } else {
            transform.width = text.length * textLabel.fontSize;
        }
        let lineCount = Math.ceil((text.length * textLabel.fontSize) / ((width * 3) / 5));
        transform.height = textLabel.fontSize * lineCount;

        // Background Node
        const bgNode = new Node("BgNode");
        const ctx = bgNode.addComponent(Graphics);
        ctx.roundRect(-transform.width / 2, -transform.height / 2, transform.width, transform.height, 20);
        ctx.fillColor = bg_color;
        ctx.fill();

        bgNode.addChild(textNode);

        // Set Toast Position
        const bgTransform = bgNode.addComponent(UITransform);
        bgTransform.width = transform.width; // Ensure the bgNode has the correct dimensions
        bgTransform.height = transform.height;

        if (gravity === "CENTER") {
            bgNode.setPosition(0, 0, 0);
        } else if (gravity === "TOP") {
            bgNode.setPosition(0, height / 4, 0);
        } else if (gravity === "BOTTOM") {
            bgNode.setPosition(0, -height / 4, 0);
        }

        canvas.node.addChild(bgNode);

        // Create and run action
        tween(bgNode)
            .delay(duration)
            .to(0.3, { position: new Vec3(bgNode.position.x, bgNode.position.y, 0)})
            .call(() => {
                bgNode.destroy();
            })
            .start();
    }
}